﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>个性菜单设置</title>
    <link href="EasyUI/themes/default/easyui.css" rel="stylesheet" />
    <link href="EasyUI/themes/icon.css" rel="stylesheet" />
    <script src="EasyUI/jquery.min.js"></script>
    <script src="EasyUI/jquery.easyui.min.js"></script>
    <script>
        function GetQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        }
        var userId = GetQueryString("userId");
        function menuLoad() {
            $.ajax({
                type: "POST",
                url: "ashx/UserMenu.ashx",
                data: "menu=0&userid=" + userId,
                cache: false,
                success: function (data) {
                    $("#menu").empty();
                    for (var i = 0; i < data.length; i++) {
                        var menu = data[i];
                        var htmlString = '<div id="'+i+'" class="easyui-panel panel-class" title="' + menu.Name + '"><ul class="ul-class">';
                        for (var j = 0; j < menu.UserChildMenuItems.length; j++) {
                            var chileMenu = menu.UserChildMenuItems[j];
                            if (!chileMenu.Checked) {
                                htmlString = htmlString + '<li class="li-class"><input type="checkbox" name="menuId" value="' + chileMenu.MenuId + '" />' + chileMenu.MenuName + '</li>';
                            } else {
                                htmlString = htmlString + '<li class="li-class"><input type="checkbox" name="menuId" checked="checked" value="' + chileMenu.MenuId + '" />' + chileMenu.MenuName + '</li>';
                            }
                            
                        }
                        htmlString = htmlString + '</ul></div>';
                        $("#menu").append(htmlString);
                        $('#'+i).panel({
                            title: menu.Name
                        });
                    }
                    
                },
                error: function (data, error, error2) {
                    alert(error);
                    alert(error2);
                }
            });
        }

        function save() {
            var str = "";
            $("input[name='menuId']:checkbox").each(function () {
                if ($(this).is(":checked")) {
                    str += $(this).attr("value") + ",";
                }
            });
            if (str != null && str.length > 1) {
                str = str.substring(0, str.length - 1);
            }
            $.ajax({
                type: "POST",
                url: "ashx/SaveMenu.ashx",
                data: "userid=" + userId + "&menuid="+ str,
                cache: false,
                success: function (data) {
                    if (data) {
                        alert("设保存成功，重新登录系统后生效");
                    } else {
                        alert("保存失败");
                    }
                },
                error: function (data, error, error2) {
                    alert(error);
                    alert(error2);
                }
            });
        }
        $(document).ready(function () {
            menuLoad();
        });


    </script>
    <style>
        .panel-class {
            height: auto;
            padding: 10px;
            width: 100%;
            margin-bottom: 5px;
        }

        .ul-class {
            list-style: none;
        }

        .li-class {
            display: inline;
            margin-right: 10px;
        }
    </style>
</head>
<body>
    <div style="margin: 20px 0 10px 0;">
        <a href="#" class="easyui-linkbutton" onclick="save()">保存设置</a>
    </div>
    <div id="menu">
        <div id="p" class="easyui-panel panel-class" title="基础信息">
            <ul class="ul-class">
                <li class="li-class"><input type="checkbox" />管理块信息</li>
                <li class="li-class"><input type="checkbox" />管理块信息</li>
                <li class="li-class"><input type="checkbox" />管理块信息</li>
                <li class="li-class"><input type="checkbox" />管理块信息</li>
                <li class="li-class"><input type="checkbox" />管理块信息</li>
            </ul>
        </div>
    </div>
</body>
</html>